<?php

class WxhelperController extends Controller
{
	public function actionGetOauth2AccessToken()
	{
		$appid = app()->request->getParam('appid', '');
		$appsecret = app()->request->getParam('appsecret', '');
		$code = app()->request->getParam('code', '');
		
		$wxInterface = new WxInterfaceHelper($appid, $appsecret);
		
		$this->_returnAjax(
			$wxInterface->getOauth2AccessToken($code)
		);
	}
	
	public function actionGetCgibinAccessToken()
	{
		$appid = app()->request->getParam('appid', '');
		$appsecret = app()->request->getParam('appsecret', '');
		$refresh = app()->request->getParam('refresh', false);
		$from = app()->request->getParam('from', false);

		$wxInterface = new WxInterfaceHelper($appid, $appsecret);

		$cgi_accesstoken = $wxInterface->getCgibinAccessToken($refresh);

        $info = 'FROM:'.$from.';appid:'.$appid.';appsecret'.$appsecret.';refresh'.$refresh.var_export($cgi_accesstoken,true);

        $this->_cgiTokenLog($info);

		$this->_returnAjax(
            $cgi_accesstoken
		);
	}

	protected function _cgiTokenLog($info)
    {
        $log_file = dirname(__FILE__) . '/logs/cgi_token_ajx.log';

        $info = empty($info)? '' : $info;
        $info = date('Y-m-d H:i:s - ') . $info . "\n";

        $handler = fopen($log_file, 'a+');
        fputs($handler, $info);
        fclose($handler);
    }

	public function actionGetCgibinTicket()
	{
		$appid = app()->request->getParam('appid', '');
		$appsecret = app()->request->getParam('appsecret', '');
		
		$wxInterface = new WxInterfaceHelper($appid, $appsecret);
		
		$this->_returnAjax(
			$wxInterface->getCgibinTicket()
		);
	}
	
	public function actionGetCardApiTicket()
	{
		$appid = app()->request->getParam('appid', '');
		$appsecret = app()->request->getParam('appsecret', '');
		
		$wxInterface = new WxInterfaceHelper($appid, $appsecret);
		
		$this->_returnAjax(
			$wxInterface->getCardApiTicket()
		);
	}
	
	
	
	
	
	
	
	
	public function accessRules()
	{
		return array(
			array(
				'allow',	//should be 'deny', 'allow' is used to give convenient to make test
				'users' => array('*'),
				'redirect' => '/user/login'
			)
		);
	}
}